package com.buss.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.buss.admin.entity.UserRoleEntity;
import com.buss.auth.transfer.vo.authen.AuthUserVOAdmin;
import com.buss.common.service.SysServiceI;
import org.apache.log4j.MDC;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LogFilter implements Filter {

    private final static double DEFAULT_USERID= 0.0;

    @Autowired
    private SysServiceI sysService;

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        //System.out.println("进入过滤器");
        HttpServletRequest req=(HttpServletRequest)request;
        HttpSession session= req.getSession();
        //用户的id
//            Integer userId = (Integer)session.getAttribute("userId");
        Subject currentUser = SecurityUtils.getSubject();
        AuthUserVOAdmin user = (AuthUserVOAdmin) currentUser.getSession().getAttribute("userInfoAdmin");

        if (user!=null){
            String userId = user.getId();
            System.out.println("用户id"+userId);
            MDC.put("userId", userId);

        }
        else{
            MDC.put("userId",DEFAULT_USERID);
        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {

    }

}